/*
 * commonTableModel.java
 *
 * Created on December 29, 2006, 12:19 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package kkWindows;


import javax.swing.*;

import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.table.*;
import java.util.LinkedList;
import java.util.Arrays;
import java.util.Vector;

public class commonTableModel extends AbstractTableModel
{
    private Object[][] dataVector;
    private Object[] columnIdentifiers;
    private String tableName;
    private int dataColIndex;
    public static final String FILES_TABLE="filesTable";
    public static final String DRIVES_TABLE="drivesTable";
    public static int FILES_TABLE_DATA_COLUMN_INDEX = 2;
    public static int DRIVES_TABLE_DATA_COLUMN_INDEX = 1;
    
    commonTableModel(Object[][] dataVector, Object[] columnIdentifiers,String tableName){
        this.dataVector=dataVector;
        this.columnIdentifiers=columnIdentifiers;
        this.tableName=tableName;
        if(tableName.equals(FILES_TABLE))
            dataColIndex=FILES_TABLE_DATA_COLUMN_INDEX;
        else if(tableName.equals(DRIVES_TABLE))
            dataColIndex=DRIVES_TABLE_DATA_COLUMN_INDEX;
    }
    
    commonTableModel(LinkedList filesList,Object[] columnIdentifiers,String tableName){
        this.columnIdentifiers=columnIdentifiers;
        this.tableName=tableName;
         if(tableName.equals(FILES_TABLE))
            dataColIndex=FILES_TABLE_DATA_COLUMN_INDEX;
        else if(tableName.equals(DRIVES_TABLE))
            dataColIndex=DRIVES_TABLE_DATA_COLUMN_INDEX;
        setData(filesList);
        
    }
    
    public int getColumnCount() {
        return columnIdentifiers.length;
    }
    
    public int getRowCount() {
        return dataVector.length;
    }
    
    public String getColumnName(int col) {
        return (String)columnIdentifiers[col];
    }
    
    public Object getValueAt(int row, int col) {
        return dataVector[row][col];
    }
    
    public Class getColumnClass(int c) {
        //System.out.println(c+"\n");
        
        return getValueAt(0, c).getClass();
        
    }
    
    public boolean isCellEditable(int rowIndex, int columnIndex){ 
        if(columnIndex==0 && tableName.equals("drivesTable"))
            return true;
        else if(columnIndex==1 && tableName.equals("filesTable"))
            return true;
        else
            return false;
        
    }
    public void setValueAt(Object value, int row, int col) {
        dataVector[row][col] = value;
        //System.out.println(row+","+col+" = "+value+"\n");
        fireTableCellUpdated(row,col);
    }
    
    public void setData(LinkedList filesList){
        int preLength=((dataVector==null)?0:dataVector.length);
        Object[][] dataVectorClone=new Object[filesList.size()+preLength][3];
        int i,j;
        System.out.println("Size of dataVector Clone : "+dataVectorClone.length);
        for(i=0;i<preLength;i++){
            dataVectorClone[i]=dataVector[i];
        }
        
        for(i=preLength,j=0;i<dataVectorClone.length;i++,j++){
            dataVectorClone[i][0]=Integer.toString(i+1);
            dataVectorClone[i][1]=true;
            dataVectorClone[i][2] = (String)(filesList.get(j));
            
        }
        
        dataVector=dataVectorClone;
        System.out.println("Data has been Set");
    }
    
    public boolean deleteRow(int[] index){
        int i,j=0;
        Arrays.sort(index);
        
        for(i=0;i<index.length;i++){
                System.out.println("index ["+i+"] = "+index[i]);
            }
            System.out.print("\n");
            for(i=0;i<dataVector.length;i++){
                System.out.println("dataVector ["+i+"][2] = "+dataVector[i][2]);
            }
        
        
        if((dataVector.length - index.length) ==0){
            return true;
        }
        else {
            Object dataVectorClone[][]=new Object[dataVector.length-index.length][3];
            
            
            
            for(i=0;i<dataVector.length;i++){
                
                if(Arrays.binarySearch(index,i)<0){
                    
                    dataVectorClone[j]=dataVector[i];
                    dataVectorClone[j][0]=j+1;
                    j++;
                }
            }
            
            dataVector=dataVectorClone;
            System.out.println("\nAfter deleting : \n");
            for(i=0;i<dataVector.length;i++){
                System.out.println("dataVector ["+i+"][2] = "+dataVector[i][2]);
            }
        }
        
        return false;
    }
    
    public void setFileSelected(int index,boolean option){
        dataVector[index][1]=option;
    }
    
    public Vector<String> getTargets(){
        int i;
        Vector<String> vTargets = new Vector();
        System.out.println("Table Name : "+tableName);
        System.out.println("inside commontablemodel : "+dataVector[0][dataColIndex-1]+",dataColIndex : "+dataColIndex);
        for(i=0;i<dataVector.length;i++){
            if( ( (Boolean)(dataVector[i][dataColIndex-1]) ).booleanValue() )
                vTargets.add((String)(dataVector[i][dataColIndex]));
        }
        if(vTargets.size() == 0)
            return null;
        vTargets.trimToSize();
        return vTargets;
        
    }
}